package com.wsl.webservice.client;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

/**
 * @Author:wangshuliang
 * @Description:
 * @Date:Created in 10:36 2019/5/22
 * @Modified by:
 */
public class CxfClient {
    private static String address = "http://localhost:8080/services/user?wsdl";
    //测试
    public static void main(String[] args) {
        test();
    }

  /*  *//**
     * 方式1:使用代理类工厂，需要拿到对方的接口
     *//*
    public static void test1() {
        try {
            //代理工厂
            JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
            //设置代理地址
            jaxWsProxyFactoryBean.setAddress(address);
            //添加用户密码拦截器
            jaxWsProxyFactoryBean.getOutInterceptors().
                    add(new LoginInterceptor("root","admin"));
            //设置接口类型
            jaxWsProxyFactoryBean.setServiceClass(AppService);
        }catch (Exception e){

        }
    }*/
    public static void test() {
        //创建动态客户端
        JaxWsDynamicClientFactory dynamicClientFactory = JaxWsDynamicClientFactory.newInstance();
        Client client = dynamicClientFactory.createClient(address);
        //需要密码的情况需要添加上用户名和密码
        client.getOutInterceptors().add(new LoginInterceptor("root","admin"));
        Object[] objects = new Object[0];
        try {
            System.out.println("========client"+client);
            objects =  client.invoke("getUserName","1");
            System.out.println("返回数据："+objects[0]);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
